package homewoek12月22号.demo1.d3;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) throws DocumentException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Element root = new SAXReader().read("src/com/czxy/demo1/d3/wab.xml").getRootElement();
        List<Element> slsit = root.elements("servlet");
        List<Element> smlist = root.elements("servlet-mapping");
        HashMap<String, String> map = new HashMap<>();
        for (Element e : smlist) {
            String key = e.elementText("url-pattern");
            for (Element element : slsit) {
                if (e.elementText("servlet-name").equals(element.elementText("servlet-name"))){
                    map.put(key,element.elementText("servlet-class"));
                }
            }
        }
        System.out.println(map);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入：");
        String str = sc.next();
        if (map.containsKey(str)){
            Object o = Class.forName(map.get(str)).newInstance();
            Class<?> aClass = o.getClass();
            Method doGet = aClass.getMethod("doGet");
            doGet.invoke(o);
        }else {
            System.out.println("输入有误!");
        }
    }
}
